﻿using george.Web.Helper;
using System;
using System.Text;
using System.Web;
using System.Web.UI;

namespace com.nm.Helper
{
    public static class FilpPaperHelper
    {
        public static IHtmlString GetPager(this Page page, int pageIndex, int pageCount)
        {
            var pi = pageIndex + 1;
            var sb = new StringBuilder();
            var qs = QueryStringHelper.ParseQuery(page.Request.QueryString.GetQuery());
            if (pi > 1)
            {
                qs.SetQuery("page", 1);
                sb.AppendFormat("<li><a href=\"?{0}\">&nbsp;<i class=\"fa fa-fast-backward\"></i>&nbsp;</a></li>", qs.GetQuery());
                qs.SetQuery("page", pi - 1);
                sb.AppendFormat("<li><a href=\"?{0}\">&nbsp;<i class=\"fa fa-backward\"></i>&nbsp;</a></li>", qs.GetQuery());
            }
            else
            {
                qs.SetQuery("page", 1);
                sb.AppendFormat("<li class=\"active\"><a href=\"?{0}\">&nbsp;<i class=\"fa fa-fast-backward\"></i>&nbsp;</a></li>", qs.GetQuery());
                qs.SetQuery("page", pi - 1);
                sb.AppendFormat("<li class=\"active\"><a href=\"?{0}\">&nbsp;<i class=\"fa fa-backward\"></i>&nbsp;</a></li>", qs.GetQuery());
            }
            if (pageCount < 6)
            {
                for (var i = 1; i <= pageCount; i++)
                {
                    if (pi == i)
                    {
                        sb.AppendFormat("<li class=\"{0}\"><a href=\"#\">{1}</a></li>", "active", i);
                    }
                    else
                    {
                        qs.SetQuery("page", i);
                        sb.AppendFormat("<li class=\"{0}\"><a href=\"?{2}\">{1}</a></li>", "", i, qs.GetQuery());
                    }
                }
            }
            else
            {
                var si = Math.Max(pi - 2, 1);
                var ei = Math.Min(si + 4, pageCount);
                if (si > pageCount - 4)
                {
                    si = pageCount - 4;
                }

                for (var i = si; i <= ei; i++)
                {
                    if (pi == i)
                    {
                        sb.AppendFormat("<li class=\"{0}\"><a href=\"?{1}\">{1}</a></li>", "active", i);
                    }
                    else
                    {
                        qs.SetQuery("page", i);
                        sb.AppendFormat("<li class=\"{0}\"><a href=\"?{2}\">{1}</a></li>", "", i, qs.GetQuery());
                    }
                }
            }
            if (pi < pageCount)
            {
                qs.SetQuery("page", pi + 1);
                sb.AppendFormat("<li><a href=\"?{0}\">&nbsp;<i class=\"fa fa-forward\"></i>&nbsp;</a></li>", qs.GetQuery());
                qs.SetQuery("page", pageCount);
                sb.AppendFormat("<li><a href=\"?{0}\">&nbsp;<i class=\"fa fa-fast-forward\"></i>&nbsp;</a></li>", qs.GetQuery());
            }
            else
            {
                qs.SetQuery("page", pi + 1);
                sb.AppendFormat("<li class=\"active\"><a href=\"?{0}\">&nbsp;<i class=\"fa fa-forward\"></i>&nbsp;</a></li>", qs.GetQuery());
                qs.SetQuery("page", pageCount);
                sb.AppendFormat("<li class=\"active\"><a href=\"&nbsp;{0}\">&nbsp;<i class=\"fa fa-fast-forward\"></i>&nbsp;</a></li>", qs.GetQuery());
            }
            //forward
            return new HtmlString(sb.ToString());
        }
    }
}